'use strict';

const Controller = require('egg').Controller;
class SkuController extends Controller {
  /**
     * @api {Post} /api/sku 添加商品sku
     * @apiGroup 商品规格
     * @apiParam {INTEGER}  goods_id                 商品id（必填)
     * @apiParam {FLOAT}    price                    价格（必填)
     * @apiParam {FLOAT}    sale_price               优惠价格（必填)
     * @apiParam {INTEGER}  stock_num                库存数量（必填)
     * @apiParam {INTEGER}  specification_id         规格id（必填)
     * @apiParam {STRING}   specification_name       规格名（必填)
     * @apiParam {INTEGER}  specification_attr_id    规格属性id（必填)
     * @apiParam {STRING}   specification_attr_name  规格属性名（必填)
     * @apiParam {STRING}   specification_img        规格图片
     * @apiParamExample 参数模板
     {
      "goods_id": 20,
      "price": 99,
      "sale_price": 88,
      "stock_num": 100,
      "specification_id": 1,
      "specification_name": '颜色',
      "specification_attr_id": 1,
      "specification_attr_name": '红色',
      "specification_img": 'http://shops-1251395798.cos.ap-nanjing.myqcloud.com/1600747499100-7a5e52bbe29f3142094d67cb5e822b3d.webp',
      }
     * @apiSuccessExample 成功返回
     * {
     *    "msg":'添加成功'
     * }
     *
     * @apiErrorExample 错误返回
     * {
     *  "msg":"添加失败"
     * }
     *
     */

  async create() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;

    if (new ctx.helper.checkData(this, 'goods_id', 'goods_keys').checkDataRes) {
      ctx.body = await service.shop.sku.create(dataObj);
    }
  }
  async update() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    console.log(dataObj, 'dataObj');
    if (
      new ctx.helper.checkData(
        this,
        'goods_id',
        'goods_price',
        'goods_stock',
        'goods_specs'
      ).checkDataRes
    ) {
      const id = ctx.params.id;
      ctx.body = await service.shop.sku.update(id, dataObj);
    }
  }
  async destroy() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    console.log(id);
    ctx.body = await service.shop.sku.destroy(id);
  }
  async show() {
    const { ctx, service } = this;
    const id = ctx.query.id;
    ctx.body = await service.shop.sku.show(id);
  }
  async adminShow() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    ctx.body = await service.shop.sku.adminShow(id);
  }

  async createKeyValue() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    if (new ctx.helper.checkData(this, 'keyName', 'valueName').checkDataRes) {
      ctx.body = await service.shop.sku.createKeyValue(dataObj);
    }
  }
  async getKeyValue() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    const id = ctx.query.project_id;
    ctx.body = await service.shop.sku.getKeyValue(id, dataObj);
  }
  async delKeyValue() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    ctx.body = await service.shop.sku.delKeyValue(id);
  }
  async delValue() {
    const { ctx, service } = this;
    const id = ctx.params.id;
    ctx.body = await service.shop.sku.delValue(id);
  }
  async updateKeyValue() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    ctx.body = await service.shop.sku.updateKeyValue(dataObj);
  }
}

module.exports = SkuController;
